#include "vkparser.h"
#include <QRegExp>

VkParser::VkParser(QString data) {
    qData = data;
    unreadNum = 0;
}

ParseUnreadMsgResult VkParser::parseUnreadMsgCount()
{
//Are we logged?
    if (qData.indexOf('/reg0') != -1) // This is 'register' button, only in main page
        return parseIncorrectInfo;
// Trying to get count...
    QRegExp re1("<a href=\"/inbox\">(.*)</a>", Qt::CaseInsensitive);
    re1.setMinimal(true); // to first </a>
    if (re1.indexIn(qData) == -1)
        return parseIncorrectInfo;
    QString tmp = re1.cap(1);
    QRegExp re2("<b>([^<]*)</b>");
    if (re2.indexIn(tmp) == -1)
    {
        unreadNum = 0;
        return parseOk;
    }
    tmp = re2.cap(1);
    bool ok;
    unreadNum = tmp.toInt(&ok, 10);
    if (!ok) unreadNum = 0;
    return parseOk;
}

int VkParser::getUnreadNum()
{
    return unreadNum;
}
